package com.leejw.tradeblogsystem.controller;

import com.leejw.tradeblogsystem.common.Result;
import com.leejw.tradeblogsystem.model.Article;
import com.leejw.tradeblogsystem.model.Shares;
import com.leejw.tradeblogsystem.model.User;
import com.leejw.tradeblogsystem.model.ZTShares;
import com.leejw.tradeblogsystem.service.SharesService;
import com.leejw.tradeblogsystem.service.TradeService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author: jwli3
 * @Desc: 交易管理
 * @create: 2024-08-16
 **/
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/trade")
public class TradeController {
    @Autowired
    private TradeService tradeService;
    @Autowired
    private SharesService sharesService;
    Logger logger = LoggerFactory.getLogger(TradeController.class);
    /**
     * 查询涨停表
     * */
    @ResponseBody
    @RequestMapping("/getAll")
    public List<ZTShares> queryUser() {
        log.info("开始进入查询模式……");
        List<ZTShares> all = tradeService.getAll();
        return all;
    }

    /**
     * 实现涨停股票批量导入
     * */
    @RequestMapping(value = "/import", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result importExcel(@RequestParam MultipartFile excelFile) throws Exception {
        //获取上传的文件
        logger.info("开始执行上传...");
        Result result = new Result();
        ZTShares shares = new ZTShares();
        try {
            Integer num = tradeService.importExcel(excelFile,shares);
            result.setCode(num);
            result.setMsg("导入成功！");
        } catch (Exception e) {
            result.setCode(500);
            return Result.error("导入失败!");
        }
        return result;
    }


    /**
     * 查询股票表
     * */
    @ResponseBody
    @RequestMapping("/getAllShares")
    public List<Shares> queryAllShares() {
        log.info("start!开始查询……");
        List<Shares> all = tradeService.queryAllShares();
        log.info("end!查询结束……");
        return all;
    }
    /**
     * 实现股票批量导入
     * */
    @RequestMapping(value = "/importShares", method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    public Result importShareExcel(@RequestParam MultipartFile excelFile) throws Exception {
        //获取上传的文件
        logger.info("开始导入数据...");
        Result result = new Result();
        Shares shares = new Shares();
        try {
            Integer num = tradeService.importShareExcel(excelFile,shares);
            result.setCode(num);
            result.setMsg("导入成功！");
            logger.info("导入成功...");
        } catch (Exception e) {
            logger.error(e.getMessage());
            result.setCode(500);
            return Result.error("导入失败!");
        }
        return result;
    }

    /**
     * 加入收藏
     * @param shareVO
     * @return
     */
    @ResponseBody
    @RequestMapping("/addFavorite")
    public Result addFavorite(@RequestBody Shares shareVO) {
        Shares tempVO = tradeService.getById(shareVO.getId());
        tempVO.setReason(shareVO.getReason());
        tradeService.updateById(shareVO);
        return Result.success(shareVO);
    }
}
